package com.library.controller;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.cdi.Eager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.library.dto.pojo.PageBean;
import com.library.dto.vo.MyAnswer0;
import com.library.dto.vo.MyAnswer1;
import com.library.dto.vo.SelectQuestionsInfo;
import com.library.entity.Lesson;
import com.library.entity.Questions;
import com.library.entity.Reader;
import com.library.entity.Results;
import com.library.service.ILessonService;
import com.library.service.IQuestionsService;
import com.library.utils.GetResultsUtils;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 至尊伍人组
 * @since 2018-09-30
 */
@Controller
@SessionAttributes(value= {"selectQuestionsInfoSession","lessons","trueAnswer1","trueAnswer0","testLessonId","reader","admin"})
@RequestMapping("/questions")
public class QuestionsController {
	@Autowired
	private IQuestionsService questionsServiceImpl;
	@Autowired
	private ILessonService lessonServiceImpl;
//	
	
	/**
	 * 查询按钮，重置搜索条件的session
	 * @param model
	 * @param selectQuestionsInfo
	 * @return
	 */
	@RequestMapping("admin/selectQuestionInfo")
	public String selectQuestionPage(ModelMap model ,
			SelectQuestionsInfo selectQuestionsInfo) {
		System.out.println(selectQuestionsInfo);
		int pageSize = 8;
		List<Questions> list=null;
		int pageNum=1;
		//保存查询条件的sesssion
		model.addAttribute("selectQuestionsInfoSession", selectQuestionsInfo);
		//创建条件构造器
		Wrapper<Questions> wrapper=new EntityWrapper<Questions>();
		
		if(selectQuestionsInfo.getQuestionsId()==null) {
			wrapper=wrapper.eq("type", selectQuestionsInfo.getType());
			if (selectQuestionsInfo.getLessonId()!=0) {
				wrapper=wrapper.eq("lessonId", selectQuestionsInfo.getLessonId());
			}
			//分页查找
			int count = questionsServiceImpl.selectCount(wrapper);
			PageBean<Questions> pageBean = new PageBean<Questions>(pageNum, pageSize, count);
			if (pageNum > pageBean.getTotalPage()) {
				pageNum = pageBean.getTotalPage();
				pageBean.setPageNum(pageNum);
			}
			
			list=questionsServiceImpl.selectPage(new Page<Questions>(pageNum, pageSize), wrapper).getRecords();
			pageBean.setList(list);
			model.addAttribute("bean", pageBean);
		}
		else {
			wrapper=wrapper.eq("questionsId", selectQuestionsInfo.getQuestionsId());
			PageBean<Questions> pageBean=new PageBean<Questions>(1, 1, 1);
			list=questionsServiceImpl.selectList(wrapper);
			pageBean.setList(list);
			model.addAttribute("bean", pageBean);
		}
		return "admin/adminQuestions";
	}
	
	
	/**
	 * 保存有搜索条件的分页查询
	 * @param model
	 * @param pageNum
	 * @return
	 */
	@RequestMapping("admin/selectQuestionInfo/{pageNum}")
	public String selectQuestionPage(ModelMap model ,@PathVariable(value = "pageNum")int pageNum) {
		int pageSize = 8;
		List<Questions> list=null;
		SelectQuestionsInfo selectQuestionsInfo=(SelectQuestionsInfo) model.get("selectQuestionsInfoSession");
		//创建条件构造器
		Wrapper<Questions> wrapper=new EntityWrapper<Questions>();
		
			wrapper=wrapper.eq("type", selectQuestionsInfo.getType());
			if (selectQuestionsInfo.getLessonId()!=0) {
				wrapper=wrapper.eq("lessonId", selectQuestionsInfo.getLessonId());
			}
			//分页查找
			int count = questionsServiceImpl.selectCount(wrapper);
			PageBean<Questions> pageBean = new PageBean<Questions>(pageNum, pageSize, count);
			if (pageNum > pageBean.getTotalPage()) {
				pageNum = pageBean.getTotalPage();
				pageBean.setPageNum(pageNum);
			}
			if (pageNum<=0) {
				pageBean.setPageNum(1);
			}
			list=questionsServiceImpl.selectPage(new Page<Questions>(pageNum, pageSize), wrapper).getRecords();
			pageBean.setList(list);
			model.addAttribute("bean", pageBean);
		return "admin/adminQuestions";
	}
	
	/**
	 * 问题管理的初始页面
	 * @param model
	 * @return
	 */
	@RequestMapping("admin/selectQuestion")
	public String selectQuestionPage(ModelMap model) {
		List<Lesson> lessons=lessonServiceImpl.selectList(new EntityWrapper<Lesson>());
		model.addAttribute("lessons", lessons);
		return "admin/adminQuestions";
	}
//	@RequestMapping("admin/selectInfo")
//	public String selectQuestionInfo(SelectQuestionsInfo selectQuestionsInfo) {
//		System.out.println(selectQuestionsInfo);
//		return "admin/adminQuestions";
//	}
	
	/**
	 * 学生考试创建试题
	 * @param model
	 * @param lessonId
	 * @return "reader/exam"
	 */
	@RequestMapping("reader/test")
	public String createPaper(ModelMap model,int lessonId) {
		model.addAttribute("testLessonId", lessonId);
		//创建单选题
		List<Questions> list0=questionsServiceImpl.createPaper0(lessonId);
		Map<String, Questions>questions0=new TreeMap<String, Questions>();
		List<String> myAnswer0=MyAnswer0.getName();
		//创建多选题
		List<Questions> list1=questionsServiceImpl.createPaper1(lessonId);
		Map<String, Questions>questions1=new TreeMap<String, Questions>();
		List<String> myAnswer1=MyAnswer1.getName();
		//生成答案List
		List<String> trueAnswer1=new ArrayList<String>();
		List<String> trueAnswer0=new ArrayList<String>();
		for(int i=0;i<15;i++) {
			questions0.put(myAnswer0.get(i), list0.get(i));
			trueAnswer0.add(list0.get(i).getAnswer());
		}
		for(int i=0;i<5;i++) {
			questions1.put(myAnswer1.get(i), list1.get(i));
			trueAnswer1.add(list1.get(i).getAnswer());
		}
		model.addAttribute("trueAnswer1", trueAnswer1);
		model.addAttribute("trueAnswer0", trueAnswer0);
		model.addAttribute("questions1", questions1);
		model.addAttribute("questions0", questions0);
		return "reader/exam";
		
	}
	
	/**获取答案,计算成绩,添加入数据库,返回成绩显示页面
	 * @param myAnswer0
	 * @param myAnswer1
	 * @param model
	 * @return "reader/result"
	 */
	@RequestMapping("reader/getResults")
	public String getResults(MyAnswer0 myAnswer0,MyAnswer1 myAnswer1,ModelMap model) {
//		System.out.println(myAnswer0);
//		System.out.println(myAnswer1);
		Reader reader=(Reader) model.get("reader");
//		System.out.println(reader);
		int result=0;
		List<String> trueAnswer1=(List<String>) model.get("trueAnswer1");
		List<String> trueAnswer0=(List<String>) model.get("trueAnswer0");
		List<String> answer0=GetResultsUtils.ConvertObjToMap(myAnswer0);
		String[] answer1=myAnswer1.getanswer();
		System.out.println(answer1);
		for(int i=0;i<15;i++) {
			if(trueAnswer0.get(i).equals(answer0.get(i)))
				result+=5;
		}
		for(int i=0;i<5;i++) {
			if(trueAnswer1.get(i).equals(answer1[i]))
				result+=5;
		}
//		System.out.println(results);
		//添加成绩到数据库
		Results results=new Results();
		int lessonId=(int) model.get("testLessonId");
		results.setLessonId(lessonId);
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
		results.setDate(simpleDateFormat.format(new Date()));
		results.setReaderId(reader.getReaderId());
		results.setResult(result);
		results.insert();
		model.addAttribute("result",result);
		model.addAttribute("answer0", answer0);
		model.addAttribute("answer1", answer1);
		return "reader/result";
		
	}
	
	/**
	 * 管理员添加试题
	 * @param model
	 * @param questions
	 * @return "redirect:admin/selectQuestionInfo/1"
	 */
	@RequestMapping("add")
	public String addQuestion(ModelMap model,Questions questions) {
//		System.out.println(questions);
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
		questions.setCreateTime(simpleDateFormat.format(new Date()));
		questionsServiceImpl.insert(questions);
		return "redirect:admin/selectQuestionInfo/1";
		
	}
	
	/**
	 * 管理员删除试题
	 * @param questinosId
	 * @return "redirect:admin/selectQuestionInfo/1"
	 */
	@RequestMapping("del/{questinosId}")
	public String deleteQuestion(@PathVariable int questinosId) {
		System.out.println(questinosId);
		return "redirect:admin/selectQuestionInfo/1";
	}
	
	/**管理员更新试题
	 * @param questions
	 * @return "redirect:admin/selectQuestionInfo/1"
	 */
	@RequestMapping("update")
	public String updateQuestion(Questions questions) {
		questionsServiceImpl.updateAllColumnById(questions);
		return "redirect:admin/selectQuestionInfo/1";
	}
}

